import 'package:flutter/material.dart';

import 'package:get/get.dart';
import 'package:word_app/app/modules/model/screen_adapter.dart';
import 'package:word_app/app/modules/model/storage_model.dart';
import 'package:word_app/app/modules/model/toast_model.dart';
import 'package:word_app/app/modules/tabbarVc/controllers/tabbar_vc_controller.dart';

import '../controllers/mine_controller.dart';

class MineView extends GetView<MineController> {
  const MineView({super.key});

  _showAlert(BuildContext context, int type) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text(type == 1 ? '你将注销账号' : "你将退出登录"),
          content:
              Text(type == 1 ? '注销账号后，所有相关数据都会被删除。请确认是否注销！！！' : '确定退出登录吗？'),
          actions: <Widget>[
            TextButton(
              child: const Text('取消'),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
            TextButton(
              child: const Text('确定'),
              onPressed: () {
                if (type == 2) {
                  controller.loginOut().then((value) {
                    if (value) {
                      StorageModel.deleteUserModel().then((v) {
                        TabbarVcController vc = Get.find();
                        vc.loginOut();
                      });
                    } else {
                      ToastModel.showToast("操作失败");
                    }
                  });
                } else if (type == 1) {
                  controller.deleteAccount().then((value) {
                    if (value) {
                      StorageModel.deleteUserModel().then((v) {
                        TabbarVcController vc = Get.find();
                        vc.loginOut();
                      });
                    } else {
                      ToastModel.showToast("操作失败");
                    }
                  });
                }
                // 执行一些操作
                Navigator.of(context).pop();
              },
            ),
          ],
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    MineController controller = Get.put(MineController());
    return Scaffold(
      body: Obx(() => Column(
            children: [
              Container(
                margin: EdgeInsets.only(
                    top: ScreenAdapterModel.getStatusBarHeight() + 10,
                    left: 10,
                    right: 10),
                height: 110,
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(10)),
                child: Row(
                  children: [
                    Container(
                      margin: const EdgeInsets.only(left: 15, right: 10),
                      height: 80,
                      width: 80,
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(40),
                      ),
                      child: ClipRRect(
                        borderRadius: BorderRadius.circular(40),
                        child: Image.network(
                          controller.photoStr.value,
                          fit: BoxFit.fill,
                        ),
                      ),
                    ),
                    Text(
                      controller.phoneStr.value,
                      style: const TextStyle(
                          fontWeight: FontWeight.bold, fontSize: 19),
                    )
                  ],
                ),
              ),
              Container(
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(10)),
                margin: const EdgeInsets.only(top: 20, left: 10, right: 10),
                child: Column(
                  children: controller.dataSoure.map((v) {
                    return SizedBox(
                      height: 50,
                      child: InkWell(
                        onTap: () {
                          if (v["title"] == "意见反馈") {
                            Get.toNamed("/feedback");
                          } else if (v["title"] == "退出登录") {
                            _showAlert(context, 2);
                          } else if (v["title"] == "注销") {
                            _showAlert(context, 1);
                          }
                        },
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Padding(
                              padding: const EdgeInsets.only(left: 10),
                              child: Row(
                                children: [
                                  SizedBox(
                                    width: 20,
                                    height: 20,
                                    child: Image.asset(
                                      "images/${v["img"]}",
                                      fit: BoxFit.cover,
                                    ),
                                  ),
                                  const SizedBox(width: 5),
                                  Text(v["title"])
                                ],
                              ),
                            ),
                            Padding(
                              padding: const EdgeInsets.only(right: 10),
                              child: v["title"] == "版本号"
                                  ? Text(
                                      v["bz"],
                                      style: const TextStyle(
                                          color: Colors.black,
                                          fontSize: 18,
                                          fontWeight: FontWeight.bold),
                                    )
                                  : Image.asset("images/push.png"),
                            )
                          ],
                        ),
                      ),
                    );
                  }).toList(),
                ),
              )
            ],
          )),
    );
  }
}
